package com.ktjiaoyu.controller;

import com.ktjiaoyu.entity.SysRole;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.service.SysRoleService;
import com.ktjiaoyu.utils.Constants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 黄军艳
 * @version 1.0
 * @date 2024/12/20  18:59
 */
@Controller
@RequestMapping("/main/role")
public class SysRoleController extends BaseController {
    private static final Logger LOGGER =
            LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    private SysRoleService sysRoleService;

    @RequestMapping("/list")
    public String list(Model model){
        List<SysRole> list=sysRoleService.findAll();
        model.addAttribute("list",list);
        return "/role/list";
    }

    //跳转到添加页面
    @GetMapping("/save")
    public String tosave(Model model){
        try {
            model.addAttribute("sysRole",new SysRole());
        } catch (Exception e) {
            LOGGER.error("SysRoleController中的toSave方法执行发生错误。", e);
            throw new RuntimeException(e);
        }
        return "/role/save";
    }

    //执行添加或修改操作
    @PostMapping("/save")
    public String dosave(SysRole sysRole){
        SysUser sysUser=session(Constants.SESSION_USER);
        try {
            boolean ok=false;
            if (sysRole.getId()==null){
                sysRole.setCreatedUserId(sysUser.getId());
                sysRole.setCreatedTime(new Date());
                ok=sysRoleService.insert(sysRole);
            }else {
                sysRole.setUpdatedUserId(sysUser.getId());
                sysRole.setUpdatedTime(new Date());
                ok=sysRoleService.update(sysRole);
            }
            if (ok){
                return redirect("/main/role/list");
            }
        }catch (Exception e) {
            LOGGER.error("SysRoleController 中的 doSave 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "/role/save";
    }

    //点击修改数据显示到文本框
    @GetMapping("/edit")
    public String toEdit(@RequestParam("id") Integer id,Model model){
        SysRole sysRole=null;
        try {
            sysRole=sysRoleService.getById(id);
            model.addAttribute("sysRole",sysRole);
        }catch (Exception e) {
            LOGGER.error("SysRoleController 中的 toEdit 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "/role/save";
    }

    //查重
    @GetMapping("/codeExist")
    @ResponseBody
    public Object codeExist(@RequestParam("code") String code){
        Map<String,Object> map=new HashMap<String,Object>();
        try {
            SysRole sysRole=sysRoleService.getByCode(code);
            if (sysRole!=null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e) {
            LOGGER.error("SysRoleController中的codeExist方法执行发生错误。 ", e);
            map.put("exist",-1);
            map.put("error","服务器繁忙，请稍后再试");
        }
        return map;
    }

    //删除
    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object delete(@PathVariable("id") Long id){
        Map<String,Object> map=new HashMap<>();
        SysUser sysUser=session(Constants.SESSION_USER);
        try {
            if (sysUser.getRoleId()!=id){
                boolean ok=sysRoleService.delete(id);
                if (ok){
                    map.put("code",200);
                }
            }else {
                map.put("code",501);
            }
        }catch (Exception e) {
            LOGGER.error("SysRoleController中的delete方法执行发生错误。 ", e);
            map.put("code",501);
        }
        return map;
    }
}
